package one.stand.service.impl;

import com.github.wxpay.sdk.WXPayUtil;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
import one.stand.code.RedisCode;
import one.stand.enums.CompanyVersionEnums;
import one.stand.model.CompanyVersionModel;
import one.stand.request.ComponentProgramRequest;
import one.stand.request.ComponentVerifyTicketRequest;
import one.stand.service.CodeService;
import one.stand.service.WxService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author: QIK
 * @CreateDate: 2019/5/9 14:42
 */
@Service
public class WxServiceImpl extends PublicBaseService implements WxService {


    @Override
    public String wxcallback(ComponentVerifyTicketRequest request) {
        String nonce =request.getNonce();
        String timestamp = request.getTimestamp();
        String msg_signature = request.getMsg_signature();
        String xml=request.getXml();
        try {
            if (StringUtils.isBlank(msg_signature)) {
                log.info("没有获取到微信发送过来的消息----------");
                return wxPayService.fail();
            }
            Map<String,String> map=WXPayUtil.xmlToMap(xml);
            WXBizMsgCrypt componentAccount = new WXBizMsgCrypt(componentConfig.getToken(), componentConfig.getMessagekey(), componentConfig.getAppid());
            xml = componentAccount.decryptMsg(msg_signature, timestamp, nonce, map.get("Encrypt"));
            map=WXPayUtil.xmlToMap(xml);
            log.info("解密后的xml:{}",xml);
            if(map.get("InfoType").equals("component_verify_ticket")){
                String componentVerifyTicket=map.get("ComponentVerifyTicket");
                if(!StringUtils.isBlank(componentVerifyTicket)){
                    redisUtil.set(RedisCode.getTicket(request.getAppid()),componentVerifyTicket);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS.toLowerCase();
    }
    @Autowired
    CodeService codeService;
    @Override
    public String wxcallback(ComponentProgramRequest request) {
        String nonce =request.getNonce();
        String timestamp = request.getTimestamp();
        String msg_signature = request.getMsg_signature();
        String xml=request.getXml();
        try {
            if (StringUtils.isBlank(msg_signature)) {
                log.info("没有获取到微信发送过来的消息----------");
                return wxPayService.fail();
            }
            log.info("获取解密前的postdata的xml:{}", xml);
            Map<String,String> map=WXPayUtil.xmlToMap(xml);
            WXBizMsgCrypt componentAccount = new WXBizMsgCrypt(componentConfig.getToken(), componentConfig.getMessagekey(), componentConfig.getAppid());
            xml = componentAccount.decryptMsg(msg_signature, timestamp, nonce, map.get("Encrypt"));
            map=WXPayUtil.xmlToMap(xml);
            log.info("解密后的xml:{}",xml);
            if(map.get("MsgType").equals("event")){
                CompanyVersionModel companyVersionModel=companyVersionMapper.selectAccountByAppId(new CompanyVersionModel(){{
                    setAppId(request.getAppid());
                }});
                log.info("收到审核结果通知：{}------{}",request.getAppid(),xml);
                String Event=map.get("Event");
                if("weapp_audit_success".equals(Event)){
                    log.info("小程序审核通过：{}",request.getAppid());
                    if(null!=companyVersionModel){
                        //自动发布功能  codeService.release(companyVersionModel.getVersionId(),request.getAppid());
                        companyVersionModel.setState(CompanyVersionEnums.audit_success.getCode());
                        companyVersionMapper.updateByPrimaryKeySelective(companyVersionModel);
                    }else {
                        log.info("版本信息错误");
                    }
                }else{
                    log.info("小程序审核不通过");
                    log.info("小程序审核失败原因：{}------{}",request.getAppid(),map.get("Reason"));
                    if(companyVersionModel==null){
                        log.info("版本信息错误");
                    }else {
                        companyVersionModel.setState(CompanyVersionEnums.audit_err.getCode());
                        companyVersionModel.setRemark(map.get("Reason"));
                        companyVersionMapper.updateByPrimaryKeySelective(companyVersionModel);
                        log.info("版本信息错误");
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS.toLowerCase();
    }
}
